define(["jquery"],function($){
	var servers = {};

	/**
	 * 处理基本信息
	 */
	servers.handleInfo = function(data){
		if( data.actualDays ) data.actualDays = (data.actualDays / 31.4).toFixed(1);
		if( data.correctDays ) data.correctDays = (data.correctDays / 31.4).toFixed(1);
		return data;
	}

	/**
	 * 处理评估问卷
	 */
	servers.handleQuestions = function(data){
		var arr = [], i = 0;
		for(var key in data){
			var obj = {};
			obj.index = i++;
			obj.monthNum = key;
			obj.month = key + "月智龄";
			obj.quest = data[key];
			arr.push(obj);
		}
		return arr;
	}

	/**
	 * 从评估问卷中获取显示的tab
	 */
	servers.showTabQuestions = function(list,index){
		var M = 8;
		var arr = [];
		for (var i = 8*index; i < 8*(index+1); i++) {
			if(list[i]) arr.push(list[i]);
		}
		return arr;
	}

	/**
	 * 从评估问卷中获取显示的问卷
	 */
	servers.handleShowQuestions = function(list,index){
		var arr = [];
		var N = 5;
		if( Number(index) - parseInt(N / 2) <= 0 ){
			for (var i = 0; i < N; i++) {
				arr.push( list[i] );
			}
		}else if( Number(index) + parseInt(N / 2) >= list.length - 1 ){
			for (var i = list.length - N; i < list.length; i++) {
				arr.push( list[i] );
			}
		}else{
			for (var i = Number(index) - parseInt(N / 2); i <= Number(index) + parseInt(N / 2); i++) {
				arr.push( list[i] );
			}
		}
		return arr;
	}

	/**
	 * cnbsr问卷的格式化
	 */
	servers.formatQuestion = function(list,index){
		var arr = [], headArr = [];
		headArr.push({"title":"能区/智龄"});
		var bigMovement = {}, fineAction = {}, adaptability = {}, language = {}, socialBehavior = {};
		bigMovement.title = "大运动", fineAction.title = "精细动作", adaptability.title = "适应能力", language.title = "语言", socialBehavior.title = "社交行为";
		bigMovement.quest = [], fineAction.quest = [], adaptability.quest = [], language.quest = [], socialBehavior.quest = [];
		for (var i = 0; i < list.length; i++) {
			var obj = {"title":list[i].month};
			if( list[i].index == index ) $.extend(obj,{"className":"active"});
			headArr.push(obj);
			bigMovement.quest.push(list[i].quest.bigMovement);
			fineAction.quest.push(list[i].quest.fineAction);
			adaptability.quest.push(list[i].quest.adaptability);
			language.quest.push(list[i].quest.language);
			socialBehavior.quest.push(list[i].quest.socialBehavior);
		}
		arr = [bigMovement,fineAction,adaptability,language,socialBehavior];
		return {
			"title":headArr,
			"content":arr
		};
	}

	/**
	 * 问卷和答案混合
	 */
	servers.mixtureQuestValue = function(list,value){
		for (var i = 0; i < list.length; i++) {
			for (var j = 0; j < list[i].quest.length; j++) {
				var obj = {};
				obj.quest = list[i].quest[j];
				obj.height = 80 / obj.quest.length + "px";
				list[i].quest[j] = obj;
				for (var p = 0; p < list[i].quest[j].quest.length; p++) {
					for (var k = 0; k < value.length; k++) {
						if( value[k] == list[i].quest[j].quest[p].id ){
							list[i].quest[j].quest[p].className = "active";
							break;
						}
					}
				}
			}
		}
		return list;
	}

	return servers;
});